/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.bl.AutorBlLocal;
import mx.its.dsw2.biblioteca.bl.CategoriaBl;
import mx.its.dsw2.biblioteca.bl.CategoriaBlLocal;
import mx.its.dsw2.biblioteca.bl.LibroBlLocal;
import mx.its.dsw2.biblioteca.enumeracion.EstadoLibro;
import mx.its.dsw2.biblioteca.modelo.Autor;
import mx.its.dsw2.biblioteca.modelo.Categoria;
import mx.its.dsw2.biblioteca.modelo.Libro;

/**
 *
 * @author Maq.8
 */
@ManagedBean
@SessionScoped
public class BookBean {

    //ejb
    @EJB
    private CategoriaBlLocal categoriaBl;
    @EJB
    private AutorBlLocal autorBl;
    @EJB
    private LibroBlLocal libroBl;
    //variables 
    private List<Categoria> listCategory;
    private List<Libro> listLibro;
    private List<Integer> listEstados;
    private Libro book;
    private String accion;
    private int selectedAuthor;
    private int selectedCategory;
    private int estado;

    /**
     * Creates a new instance of BookBean
     */
    public BookBean() {
        book = new Libro();
    }

    //get y set
    public List<Categoria> getListCategory() {
        
        return listCategory;
    }

    public List<Libro> getListLibro() {
        listLibro = libroBl.getAll();
        return listLibro;
    }

    public List<Integer> getListEstados() {
        listEstados=new ArrayList<Integer>();
         EstadoLibro[] listaItems=EstadoLibro.values();
         for(int x=0;x<listaItems.length;x++){
             listEstados.add(listaItems[x].ordinal());
         }
        return listEstados;
    }

    public int getEstado() {
        return estado;
    }

    public void setEstado(int estado) {
        this.estado = estado;
    }

    public int getSelectedCategory() {
        return selectedCategory;
    }

    public void setSelectedCategory(int selectedCategory) {
        this.selectedCategory = selectedCategory;
    }

    public int getSelectedAuthor() {
        return selectedAuthor;
    }

    public void setSelectedAuthor(int selectedAuthor) {
        this.selectedAuthor = selectedAuthor;
    }

    public Libro getBook() {
        return book;
    }

    public void setBook(Libro book) {
        this.book = book;
    }
    //acciones

    public String procesarSeleccion() {
        listCategory = new ArrayList<Categoria>();
        book.setIdAutor(autorBl.findById(selectedAuthor));
        return "BookConfirm";
    }

    public String processAction() {
        book.setIdAutor(autorBl.findById(selectedAuthor));
        return "BookConfirm";
    }

    public String procesarAction() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            libroBl.register(book, listCategory);
        } else {
            if (accion.equalsIgnoreCase("Editar")) {
                libroBl.modify(book, listCategory);
            } else {
                if (accion.equalsIgnoreCase("Eliminar")) {
                    libroBl.delete(book);
                }
            }
        }

        accion = "";
        return "BooksList";
    }

    public String prepararCancelar() {
        String regresa = "";
        if (accion.equalsIgnoreCase("Eliminar")) {
            regresa = "BooksList";

        }
        if (accion.equalsIgnoreCase("Editar") || accion.equalsIgnoreCase("Nuevo")) {
            regresa = "BookCreateEdit";
        }
        return regresa;
    }
    //listeners

    public void prepararNuevo(ActionEvent e) {
        listCategory = new ArrayList<Categoria>();
        accion = "Nuevo";
        book = new Libro();
    }

    public String prepararEditar() {
       accion="Editar";
       listCategory=categoriaBl.getListByBook(book);
       return "BookCreateEdit";
    }

    public void prepararEliminar(ActionEvent e) {
        accion = "Eliminar";
        int index = Integer.parseInt(e.getComponent().getClientId().split(":")[2]);
        book = listLibro.get(index);

    }

    public void agregarCategoria(ActionEvent evt) {
        if (!listCategory.contains(categoriaBl.findById(selectedCategory))) {
            listCategory.add(categoriaBl.findById(selectedCategory));
        } else {
        }
    }

    public void eliminarCategoriaDeLista(ActionEvent evt) {//createedit
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        listCategory.remove(index);

    }
}
